package com.epsoft.odin7cj.flow.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;

/**
 * DESC: Bpmn图片处理 <br>
 * @author 胡义振
 * @date 2020年5月21日
 */
public class BpmnPicUtils {

	public static String convertToPng(String svg, String pngFilePath) {
		String errorMessage = "";
		File file = new File(pngFilePath);
		FileOutputStream outputStream = null;
		try {
			file.createNewFile();
			outputStream = new FileOutputStream(file);
			svgConvertToPng(svg, outputStream);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return errorMessage;
	}

	/**
	 * DESC: SVG转换成png输出流
	 * @auther 胡义振
	 * @date 2020年5月21日
	 * @param svgCode
	 * @param outputStream
	 * @return
	 */
	public static void svgConvertToPng(String svgCode, OutputStream outputStream) {
		try {
			byte[] bytes = svgCode.getBytes("utf-8");
			PNGTranscoder t = new PNGTranscoder();
			TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
			TranscoderOutput output = new TranscoderOutput(outputStream);
			t.transcode(input, output);
			outputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
